package cst.neu.yuejian_31.model;

import cst.neu.yuejian_31.bean.FeedBackBean;
import cst.neu.yuejian_31.iface.FeedBackIface;
import cst.neu.yuejian_31.iface.FeedBackListener;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.FeedBackService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by xxe on 2017/11/1.
 */

public class FeedBackModel implements FeedBackIface {
    private Retrofit retrofit;
    public FeedBackModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void feedBack(String userId, String conTant, final FeedBackListener feedBackListener) {
        FeedBackService feedBackService=retrofit.create(FeedBackService.class);
        Call<FeedBackBean>call=
                feedBackService.feedBack(userId,conTant);
        call.enqueue(new Callback<FeedBackBean>() {
            @Override
            public void onResponse(Call<FeedBackBean> call, Response<FeedBackBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    feedBackListener.onSuccess(response.body());
                }else {
                    feedBackListener.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<FeedBackBean> call, Throwable t) {
                feedBackListener.onFail(t.toString());
            }
        });

    }
}
